﻿using System;
using System.Web;
using System.IO;
public class UploadHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";

        //Client objClient = Client.GetCurrent();
        //if (objClient == null)
        //{
        //    context.Response.Write("1");
        //    return;
        //}
        HttpPostedFile file = context.Request.Files["Filedata"];
        string uploadPath =
            HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\" + DongDong.Common.Misc.ConvertToShortDateTime(System.DateTime.Now, "yyyyMMdd") + "\\";

        string newfilename = "";

        if (file != null)
        {
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            string fileName = DongDong.Common.Misc.MaxCharacters(HttpContext.Current.Server.HtmlEncode(file.FileName), 30).ToLower();
            string extension = System.IO.Path.GetExtension(fileName).ToLower();
            newfilename = DongDong.Common.Misc.MaxCharacters(System.Guid.NewGuid().ToString(), 5) + "-" + fileName.Replace(extension, "") + extension;
            file.SaveAs(uploadPath + newfilename);

            AttachFile objAttaceFile = new AttachFile();
            //objAttaceFile.SaveSrc = DongDong.Common.Misc.ConvertToShortDateTime(System.DateTime.Now, "yyyyMMdd") + "\\" + newfilename;
            //objAttaceFile.TaskID = Task.GetMaxID() + 1;
            //objAttaceFile.UserID = 0;
            //objAttaceFile.Title = fileName;
            objAttaceFile.Add();
            //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
            context.Response.Write("1");
        }
        else
        {
            context.Response.Write("0");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
